Get visitor's location details from IP address in php

Course- PHP Tutorial >

If you want to track your visitor's location details like city, state, country etc then you can use the following script. In PHP we can easily get the visitors/ user IP address by using $_SERVER['REMOTE_ADDR'] option. But you can't get other information like country, city, state etc directly in PHP. Codehelper.io provides the solution for this.

How to get user IP Address in PHP

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>

 

How to get user/ visitor Location in PHP

<?php
require_once("userip/ip.codehelper.io.php");
require_once("userip/php_fast_cache.php");

$_ip = new ip_codehelper();

$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

$guest_ip   = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city  = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];

echo "IP Address: ". $guest_ip. "<br/>";
echo "Country: ". $guest_country. "<br/>";
echo "State: ". $guest_state. "<br/>";
echo "City: ". $guest_city. "<br/>";

?>


The above code provides the information like ip, Continent Code, Continent Name, Country Code in 2 digit, Country Code in 3 digit,  Country short code, Country Name, Region / State Name, City Name, City Latitude, City Longitude,  Country Latitude, Country Longitude, Local Time Zone, Calling Code, Population, Area in  Sq Km, Capital, Electrical, Languages, Currency and Flag image.

$ip 			= $visitor_location['IP'];
$Continent_Code 	= $$$visitor_location['ContinentCode'];
$Continent_Name 	= $visitor_location['ContinentName'];
$Country_Code2 		= $visitor_location['CountryCode2'];
$Country_Code3 		= $visitor_location['CountryCode3'];
$Country 		= $visitor_location['Country'];
$Country_Name 		= $visitor_location['CountryName'];
$State_Name 		= $visitor_location['RegionName'];
$City_Name 		= $visitor_location['CityName'];
$City_Latitude 		= $visitor_location['CityLatitude'];
$City_Longitude 	= $visitor_location['CityLongitude'];
$Country_Latitude 	= $visitor_location['CountryLatitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$Country_Longitude 	= $visitor_location['CountryLongitude'];
$LocalTimeZone 		= $visitor_location['LocalTimeZone'];
$Calling_Code 		= $visitor_location['CallingCode'];
$Population 		= $visitor_location['Population'];
$Area_SqKm 		= $visitor_location['AreaSqKm'];
$Capital 		= $visitor_location['Capital'];
$Electrical 		= $visitor_location['Electrical'];
$Languages 		= $visitor_location['Languages'];
$Currency 		= $visitor_location['Currency'];
$Flag 			= $visitor_location['Currency'];